class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

// 遍历放置在数组中，通过pop方法批量放进最新的链表中
function reverseList(head: ListNode | null): ListNode | null {
  const arr = [];
  let current = head;
  while (current) {
    arr.push(current.val);
    current = current.next;
  }
  if (arr.length === 0) return null;
  const newHeader = new ListNode(arr.pop(), null);
  let newCurrent = newHeader;
  while (arr.length) {
    newCurrent.next = new ListNode(arr.pop(), null);
    newCurrent = newCurrent.next;
  }
  return newHeader;
}

const head1 = new ListNode(1, new ListNode(2, new ListNode(3)));

console.dir(reverseList(head1));

export {};
